<?php
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
/*$authid = 1;
if ($_POST['click'] == "1")
{
    $sql_imba = "SHOW COLUMNS FROM field_data LIKE 'var%'";
    $result_imba = mysqli_query($con, $sql_imba);
    $i=0;
    $sql_imba_ = 'INSERT INTO field_data (`id`,`auth_id`,`date_create`, `date_update`,`action_id`,`field_category_action_id`';

    $pvalues ="";
    while($row_imba = mysqli_fetch_array($result_imba))
    {
        $sql_imba_ = $sql_imba_.',`'.$row_imba['Field'].'`';
        $imba = '"'.$row_imba['Field'].'"';
        //echo $imba.'|'.$_POST[$imba].'<br>';
        if (stripos($row_imba['Type'],"var") !== false)
        {
            $pvalues = $pvalues.',"'.$_POST[$row_imba['Field']].'"';
        }
        if (stripos($row_imba['Type'],"int") !== false)
        {
            $pvalues = $pvalues.','.intval($_POST[$row_imba['Field']]);
        }
        if (stripos($row_imba['Type'],"blob") !== false)
        {
            if($_FILES[$row_imba['Field']]['size'] > 0)
            {
                //$fileName = $_FILES['userfile']['name'];
                $tmpName  = $_FILES[$row_imba['Field']]['tmp_name'];
                //$fileSize = $_FILES['userfile']['size'];
                //$fileType = $_FILES['userfile']['type'];

                $fp      = fopen($tmpName, 'r');
                $content = fread($fp, filesize($tmpName));
                $content = addslashes($content);
                fclose($fp);
            }

            $pvalues = $pvalues.',\''.$content.'\'';
        }

    }
    $sql_imba_ = $sql_imba_ . ') VALUES (NULL,'.$authid.',CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),'.intval($_POST['action_id']).','.intval($_POST['fcai']);
    $sql_imba_ = $sql_imba_. $pvalues.')';
   // echo $sql_imba_.'<br>';
    mysqli_query($con,$sql_imba_) or die('Error, query failed!!!');
    //$result_imba_ =  mysqli_query($con, $sql_imba_);
}
*/
//if ($_GET['id_ad'] == "0")

//{

 echo '


<ul class="breadcrumb">
    <li><a href="login.php?page=user_panel">Καρτέλα Χρήστη</a> <span class="divider">/</span></li>
    <li ><a href="login.php?page=user_panel_aggelies">Αγγελίες</a> <span class="divider">/</span></li>
    <li class="active">Εισαγωγή  </li>
</ul>


 <h5> Διάλεξε από το παρακάτω μενού την κατηγορία στην οποία θέλεις να ενταχθεί η αγγελία σου    </h5>';


if (empty($_GET['id']))
{
    $yo = 0;
}
else
{
 $yo = intval($_GET['id']);
}
if (empty($_GET['cat_id']))
{
    $parent_id = 0;
    $parent_name = "Κατηγορίες";
}
else
{
    $parent_id = intval($_GET['cat_id']);
}

$sql_ = 'SELECT category_matrix.id as cmi,category_matrix.category_id as cmci, category_matrix.parent_id as cmpi, category_matrix.ordering, category.id, category.name  FROM category_matrix,category WHERE category_matrix.category_id = category.id AND category_matrix.parent_id='.$parent_id;
$result_ = mysqli_query($con, $sql_);
$row_ = mysqli_fetch_array($result_);
//echo $sql_;
if (isset($row_))
{
$sql__ ='SELECT * FROM category WHERE category.id='.$parent_id;
$result__ = mysqli_query($con, $sql__);
$row__ = mysqli_fetch_array($result__);

if ($parent_id <> 0)
{
$sql = 'SELECT category_matrix.id as cmi,category_matrix.category_id as cmci, category_matrix.parent_id as cmpi, category_matrix.ordering, category.id, category.name  FROM category_matrix,category WHERE category_matrix.category_id = category.id AND category_matrix.category_id='.intval($_GET['pid']);
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
if ($row['cmci']==0)
{
    $cname = "Κατηγορίες";
}
else
{
    $cname = $row['name'];
}
    echo '<a class="btn" href="?page=admin_advert/advert_data&cat_id='.$row['cmci'].'">'.$cname.'</a>';
$parent_name = $row__['name'];
}

echo '<div class="dropdown">';
echo '<a class="btn dropdown-toggle" href="?page=admin_advert/advert_data&cat_id='.$parent_id.'" data-toggle="dropdown">'.$parent_name.'<span class="caret"></span>    </a>';
echo '<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">';
$result = mysqli_query($con, $sql_);
while ($row = mysqli_fetch_array($result)) {
    //print_r ($row);
    //echo "\n";
    echo '<li><a tabindex="-1" href="?page=admin_advert/advert_data&cat_id='.$row['cmci'].'&pid='.$row['cmpi'].'">'.$row['name'].'</a></li>';
}
echo '</ul>';
echo '</div>';
}
else
{
 //   $sql_f1 = 'SELECT category_action.id as caid,category_action.category_id,category_action.action_group_id,category.name as cn,category.id,action_group.id, action_group.name as agn FROM category_action,category,action_group WHERE category_action.category_id = '.$parent_id.' AND category_action.action_group_id = action_group.id group by category.name';
 //  $sql_f1 = 'SELECT field_category_action.field_id as fcafi, category_action.id as cai, field_category_action.category_action_id as fcacai, field_category_action.ordering as fcao, field.name as fn, category.name as cn FROM field_category_action, category_action, field, category WHERE field_category_action.field_id = field.id AND field_category_action.category_action_id=category_action.id AND category_action.category_id='.$parent_id.' ORDER BY field_category_action.ordering';
    $sql_f1 = 'SELECT field_category_action.id as fcai, field_category_action.field_id as fcafi, category_action.id as cai, field_category_action.category_action_id as fcacai, field_category_action.ordering as fcao, field.name as fn, field.id as fi, category.name as cn, field.parameters as fpara FROM field_category_action, category_action, field, category WHERE field_category_action.field_id = field.id AND field_category_action.category_action_id=category_action.id AND category_action.category_id=category.id AND category.id='.$parent_id.' ORDER BY field_category_action.ordering';
    $result_f1 = mysqli_query($con, $sql_f1);
    $result_f = mysqli_query($con, $sql_f1);
    //echo "Fields<br>";
    //echo $sql_f1;
    $form = '<div class="hero-unit">
         ';
    //echo '<table>';
    $row_f = mysqli_fetch_array($result_f);

    $form = $form . '<form id="form_advert_insert" method="post" enctype="multipart/form-data" action="?page=admin_advert/advert_data_ins">';
    $form = $form . '<input name="fcai" type="hidden" value="'.$row_f['fcai'].'"/>';
    while($row_f1 = mysqli_fetch_array($result_f1))
    {

        if ($row_f1['fpara'] == "TEXT")
        {
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            $form = $form . '<input type="text" name="var'.$row_f1['fi'].'" value="" />';
        }
        else if ($row_f1['fpara'] == "MEDIA")
        {
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            $form = $form . '<input type="file" name="var'.$row_f1['fi'].'" value="" />';
        }
        else if ($row_f1['fpara'] == "PASSWORD")
        {
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            $form = $form . '<input type="password" name="var'.$row_f1['fi'].'" value="" />';
        }
        else if ($row_f1['fpara'] == "TEXTAREA")
        {
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            $form = $form . '<textarea COLS="40" ROWS="6" name="var'.$row_f1['fi'].'">  </textarea>';
        }
        else if ($row_f1['fpara'] == "RADIOBOX")
        {
            $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
            $result_f2 = mysqli_query($con, $sql_f2);
            //echo $sql_f2;
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            while($row_f2 = mysqli_fetch_array($result_f2))
            {
                $form = $form . '<input type="radio" name="var'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />'.$row_f2['name'].'<br>';;
            }
        }
        else if ($row_f1['fpara'] == "CHECKBOX")
        {
            $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
            $result_f2 = mysqli_query($con, $sql_f2);
            //echo $sql_f2;
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            while($row_f2 = mysqli_fetch_array($result_f2))
            {
                $form = $form . '<input type="checkbox" name="var'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />'.$row_f2['name'].'<br>';
            }
        }
        else if ($row_f1['fpara'] == "DROPDOWNBOX")
        {
            $sql_f2 = 'SELECT field_matrix.id as fmi,field_matrix.field_id as fmfi, field_matrix.parent_id as fmpi, field_matrix.ordering as fmo,field.id,field.name FROM field_matrix, field WHERE field_matrix.field_id=field.id AND field_matrix.parent_id ='.intval($row_f1['fi']).' ORDER BY field_matrix.ordering';
            $result_f2 = mysqli_query($con, $sql_f2);
            //echo $sql_f2;
            $form = $form . '<h5>'.$row_f1['fn'].'</h5>';
            $form = $form . '<select name="var'.$row_f1['fi'].'">';
            $form = $form . '<option selected value="">Επιλέξτε</option>';
            while($row_f2 = mysqli_fetch_array($result_f2))
            {


                $form = $form . '<option value="'.$row_f2['fmfi'].'">'.$row_f2['name'].'</option>';
                //$form = $form . '<input type="checkbox" name="val'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />';
            }
            $form = $form.'</select>';
        }

        //else{
        //echo '<tr><td>'.$row_f1['fcafi'].'</td><td>'.$row_f1['cai'].'</td><td>'.$row_f1['fcacai'].'</td><td>'.$row_f1['fcao'].'</td><td>'.$row_f1['fn'].'</td><td>'.$row_f1['cn'].'</td><td>'.$row_f1['fpara'].'</td></tr>';
        //}//<td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_matrix_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
    }
    $sql_f2 = 'SELECT category_action.category_id, category_action.action_group_id as caagi, action.id, action.name, action.action_group_id, action.ordering FROM category_action, action WHERE category_action.action_group_id=action.action_group_id AND category_action.category_id='.$parent_id.' ORDER BY action.ordering';
    $result_f2 = mysqli_query($con, $sql_f2);
    //echo $sql_f2;
    $form = $form . '<h5>Ενέργεια</h5>';
    $form = $form . '<select name="action_id">';
    $form = $form . '<option selected value="">Επιλέξτε</option>';
    while($row_f2 = mysqli_fetch_array($result_f2))
    {


        $form = $form . '<option value="'.$row_f2['id'].'">'.$row_f2['name'].'</option>';
        //$form = $form . '<input type="checkbox" name="val'.$row_f1['fi'].'" value="'.$row_f2['fmfi'].'" />';
    }
    $form = $form.'</select>';
    $form = $form . '<input name="click" type="hidden" value="1"/> <br />';
    $form = $form . '<input type="submit" class="btn btn-info btn-small" value="Προσθήκη αγγελίας"/>';
    $form = $form . '</center></form> </div>';
    echo $form;
    //echo '</table>';
}
/*
}
elseif ($_GET['id_ad'] == "1") {

    $sql_imba = "SHOW COLUMNS FROM field_data LIKE 'var%'";
    $result_imba = mysqli_query($con, $sql_imba);
    echo $sql_imba;
    while($row_imba = mysqli_fetch_array($result_imba))
    {
    $field_id = substr($row_imba['Field'],3);
    }
    $result = mysqli_query($con, " SELECT * FROM advert_data WHERE auth_id='.$auth_id.' group by name ");
    echo '<table border="1">';
    while($row = mysqli_fetch_array($result))
    {
        echo '<tr><td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['divname'].'</td><td>[ <a href="?page=admin_advert/field_edit&id_ad=2&id='.$row['id'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/field_edit&id_ad=3&id='.$row['id'].'">Διαγραφή</a> ]</td></tr>';
    }
    echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>*/